package ZooDBMS;
import java.util.*;

public class Main {
    public static void main(String[] args) {


        //初始化动物
        int animalSize=1000;

        HashSet <String>strSet1=new HashSet<>();
        String temp1;
        char[] animalName=new char[5];
        char[] character={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        while(strSet1.size()<animalSize){
            for(int i=0;i<5;i++){
                animalName[i]=character[(int) (Math.random()*26)];
            }
            temp1=new String(animalName);
            strSet1.add(temp1);
        }


        Animal[] animals=new Animal[animalSize];


        String[] name1=new String[animalSize];
        int index=0;
        for (String s : strSet1) {
            name1[index++] = s;
            //System.out.println(name1[index-1]);
        }

        String[] aniClass=new String[4];
        aniClass[0]="大象";
        aniClass[1]="长颈鹿";
        aniClass[2]="老虎";
        aniClass[3]="猴子";

        String[] category1=new String[animalSize];
        int[] age1=new int[animalSize];
        int[] weight1=new int[animalSize];
        int[] height1=new int[animalSize];
        for(int i=0;i<animalSize;i++){
            category1[i]=aniClass[(int)(Math.random()*4)];
            age1[i]=(int)(Math.random()*100+1);
            weight1[i]=(int)(Math.random()*4980+20);
            height1[i]=(int)(Math.random()*490+10);
            animals[i]=new Animal(name1[i],category1[i],age1[i],weight1[i],height1[i]);
        }


        //初始化管理员
        int adminSize=100;

        HashSet <String>strSet2=new HashSet<>();
        String temp2;
        char[] adminName=new char[7];
        while(strSet2.size()<adminSize){
            for(int i=0;i<adminName.length;i++){
                adminName[i]=character[(int) (Math.random()*26)];
            }
            temp2=new String(adminName);
            strSet2.add(temp2);
        }

        Admin[] admins=new Admin[adminSize];


        String[] name2=new String[adminSize];
        index=0;
        for (String s : strSet2) {
            name2[index++] = s;
        }

        int[][] ad=new int[adminSize][10];
        int temp;
        HashSet <Integer>adSet=new HashSet<>();
        while(adSet.size()<animalSize){

            temp=(int) (Math.random()*1000);
            adSet.add(temp);
        }

        Iterator<Integer> it=adSet.iterator();
        for(int i=0;i<adminSize;i++){
            for(int j=0;j<10;j++){
                if(it.hasNext()){
                    ad[i][j]=it.next();
                }

            }
        }


        int[] age2=new int[adminSize];

        for(int i=0;i<adminSize;i++){
            age2[i]=(int)(Math.random()*45+20);
            admins[i]=new Admin(name2[i],age2[i],ad[i]);
        }

        //实例化一个zoo1对象
        Zoo zoo1=new Zoo("杭州动物园","杭州市西湖区虎跑路40号",animals,admins);

        for(int i=0;i<animalSize;i++){
            printAnimal(zoo1.animals[i]);
        }
        for(int i=0;i<adminSize;i++){
            printAdmin(zoo1.admins[i], zoo1.animals);
        }



        // 输入动物数据（共10条）
        Scanner scanner = new Scanner(System.in);
        Animal[] newAnimals = new Animal[10];
        for (int i = 0; i < 10; i++) {
            System.out.println("请输入第 " + (i + 1) + " 条动物的数据（名称 类别 年龄 体重 高度）:");
            String name = scanner.next();
            String category = scanner.next();
            int age = scanner.nextInt();
            int weight = scanner.nextInt();
            int height = scanner.nextInt();
            newAnimals[i] = new Animal(name, category, age, weight, height);
        }

        // 更新随机生成的动物数据
        System.arraycopy(newAnimals, 0, animals, 0, 10);

        // 输出更新后的新动物数据
        for (int i = 0; i < 10; i++) {
            printAnimal(animals[i]);
        }

        // 统计动物种类出现的次数
        Map<String, Integer> animalCount = new HashMap<>();
        for (Animal animal : animals) {
            animalCount.put(animal.Category, animalCount.getOrDefault(animal.Category, 0) + 1);
        }

        // 输出动物种类出现的次数
        for (Map.Entry<String, Integer> entry : animalCount.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue() + "只");
        }

        // 将所有的动物按照年龄升序排列，若存在年龄相同的动物，这些相同的动物内部再按照体重升序排列
        Arrays.sort(animals, (a1, a2) -> {
            if (a1.age != a2.age) {
                return a1.age - a2.age;
            } else {
                return a1.weight - a2.weight;
            }
        });

        // 打印排序后的结果
        for (Animal animal : animals) {
            printAnimal(animal);
        }

        // 关闭scanner
        scanner.close();

    }

    static public void printAdmin(Admin admin,Animal[] animals){
        System.out.println("name:"+admin.name+" age:"+admin.age+" admin:");
        for(int i=0;i<10;i++){
            printAnimal(animals[admin.admin[i]]);
        }
    }


    static public void printAnimal(Animal animal){
        System.out.println("Category:"+animal.Category+" name:"+animal.name+" "+animal.age+" weight:"+animal.weight+" height:"+animal.height);
    }



}

